package game;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import dao.DbConnection;

import jgame.Highscore;

import jgame.JGColor;
import jgame.JGFont;
import jgame.JGObject;
import jgame.JGPoint;
import jgame.platform.StdGame;

public class SpaceRunIII extends StdGame {
	JGFont scoring_font = new JGFont("Arial", 0, 8.0D);
	private URL url;
	private URLConnection servletConnection;
	private String playerImg;

	public static void main(String[] paramArrayOfString) {
		new SpaceRunIII(parseSizeArgs(paramArrayOfString, 0));
	}

	public SpaceRunIII() {
		initEngineApplet();
	}

	public SpaceRunIII(JGPoint paramJGPoint) {
		initEngine(paramJGPoint.x, paramJGPoint.y);
	}

	public void initCanvas() {
		setCanvasSettings(20, 15, 16, 16, null, null, null);
		if (isMidlet())
			setScalingPreferences(0.75D, 1.333333333333333D, 0, 7, 0, 7);
	}

	public void initGame() {
		try {
			url = new URL(getCodeBase(), "PlayerAvatar");
			servletConnection = url.openConnection();
			servletConnection.setDoInput(true);
			servletConnection.setDoOutput(true);
			servletConnection.setUseCaches(false);
			servletConnection.setDefaultUseCaches(false);
			servletConnection.setRequestProperty("Content-Type", "text/plain");
			InputStreamReader input = new InputStreamReader(
					servletConnection.getInputStream());
			BufferedReader in = new BufferedReader(input);
			playerImg = new String();
			playerImg = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		defineMedia("space_run.tbl");
		defineImage("ship", "p", 0, "gfx/ship32x16.gif", "-", 4, 4, 22, 10);
		// defineImage("ship", "p", 0, playerImg, "-", 4, 4, 22, 10);
		if (isMidlet()) {
			setFrameRate(18.0D, 1.0D);
			setGameSpeed(2.0D);
		} else {
			setFrameRate(45.0D, 1.0D);
		}
		this.lives_img = "ship";
		this.startgame_ingame = true;
		this.leveldone_ingame = true;
		this.title_color = JGColor.yellow;
		this.title_bg_color = new JGColor(140, 0, 0);
		this.title_font = new JGFont("Arial", 0, 20.0D);
		setHighscores(10, new Highscore(0, "nobody"), 15);
		this.highscore_title_color = JGColor.red;
		this.highscore_title_font = new JGFont("Arial", 0, 20.0D);
		this.highscore_color = JGColor.yellow;
		this.highscore_font = new JGFont("Arial", 0, 16.0D);
	}

	public void defineLevel() {
		removeObjects(null, 0);
		int n;
		switch (this.stage % 2) {
		case 0:
			this.leveldone_ingame = true;
			setPFSize(150, 40);
			setPFWrap(false, false, 0, 0);
			int i = 11 - this.level / 2;
			int j = pfTilesY() / 2 - i / 2;
			fillBG("#");
			int k = 15;
			int m = 0;
			for (n = 0; n < pfTilesX(); n++) {
				for (int i1 = j; i1 < j + i; i1++) {
					setTile(n, i1, "");
				}
				if (k > 0) {
					k--;
				} else {
					if (random(0.0D, 5.0D) < 1.0D) {
						new JGObject("enemy", true, tileWidth() * n,
								tileHeight() * (m + i / 2), 2, "enemy", 0, 0,
								16, 16, -1.0D, 0.0D, -4);
					}

					if (random(0.0D, 5.0D) < 1.0D) {
						new JGObject("pod", true, tileWidth() * n, tileHeight()
								* (m + random(2, i - 3, 1)), 4, "pod", 0, 0,
								14, 14, 0.0D, 0.0D, -4);
					}

					m = j;
					j += random(-1, 1, 2);
					if (j < 1)
						j = 1;
					if (j + i >= pfTilesY() - 1)
						j = pfTilesY() - i - 1;
				}
			}
			break;
		case 1:
			this.leveldone_ingame = false;
			setPFSize(21, 16);
			setPFWrap(true, true, -8, -8);
			fillBG("");
			for (n = 0; n < 5 + this.level / 2; n++) {
				new BombDropper();
				new JGObject("pod", true, random(pfWidth() / 2, pfWidth()),
						random(0.0D, pfHeight()), 4, "pod", 0, 0, 14, 14, 0.0D,
						random(0.5D, 1.2D), -1);
			}

		}

		new Player(0.0D, pfHeight() / 2, 3.0D);
	}

	public void initNewLife() {
		defineLevel();
	}

	public void startGameOver() {
		removeObjects(null, 0);
	}

	public void doFrameInGame() {
		drawString(getCodeBase().toString(), (int) getObject("player").x + 100,
				(int) getObject("player").y, -1);
		drawString(url.toString(), 100, 150, -1);
		drawString(playerImg, 100, 100, -1);
		moveObjects();
		checkCollision(6, 1);
		checkBGCollision(1, 1);
		if ((this.stage % 2 == 1) && (countObjects("pod", 0) == 0))
			levelDone();
		setViewOffset((int) getObject("player").x + 100,
				(int) getObject("player").y, true);
	}

	public void incrementLevel() {
		if (this.level < 10)
			this.level += 1;
		this.stage += 1;
	}

	public void paintFrameLifeLost() {
		setColor(this.title_bg_color);
		drawRect(160.0D, 50.0D, this.seqtimer * 7.0D, this.seqtimer * 5.0D,
				true, true, false);

		int i = posWalkForwards(-24, viewHeight(), this.seqtimer, 80, 50, 20,
				10);

		drawString("You're hit !" + getAppletContext().toString(), 160.0D, 200,
				0,
				getZoomingFont(this.title_font, this.seqtimer, 0.2D, 0.025D),
				this.title_color);
	}

	public void paintFrameGameOver() {
		setColor(this.title_bg_color);
		setStroke(1.0D);
		drawString(
				"Game Over !",
				160.0D,
				50.0D,
				0,
				getZoomingFont(this.title_font, this.seqtimer, 0.2D,
						0.008333333333333333D), this.title_color);
	}

	public void paintFrameStartGame() {
		drawString("Get Ready!", 160.0D, 50.0D, 0,
				getZoomingFont(this.title_font, this.seqtimer, 0.2D, 0.0125D),
				this.title_color);
	}

	public void paintFrameStartLevel() {
		drawString("Stage " + (this.stage + 1), 160.0D, 50.0D + this.seqtimer,
				0,
				getZoomingFont(this.title_font, this.seqtimer, 0.2D, 0.0125D),
				this.title_color);
	}

	public void paintFrameLevelDone() {
		drawString(
				"Stage " + (this.stage + 1) + " Clear !",
				160.0D,
				50.0D,
				0,
				getZoomingFont(this.title_font, this.seqtimer + 80.0D, 0.2D,
						0.0125D), this.title_color);
	}

	public void paintFrameTitle() {
		drawString(
				"Space Run III",
				160.0D,
				50.0D,
				0,
				getZoomingFont(this.title_font, this.seqtimer + 20.0D, 0.3D,
						0.03D), this.title_color);

		drawString(
				"Press " + getKeyDesc(this.key_startgame) + " to start",
				160.0D,
				120.0D,
				0,
				getZoomingFont(this.title_font, this.seqtimer + 5.0D, 0.3D,
						0.03D), this.title_color);

		if (!isMidlet())
			drawString(
					"Press " + getKeyDesc(this.key_gamesettings)
							+ " for settings",
					160.0D,
					160.0D,
					0,
					getZoomingFont(this.title_font, this.seqtimer, 0.3D, 0.03D),
					this.title_color);
	}

	class BombDropper extends JGObject {
		public BombDropper() {
			super("", true,
					SpaceRunIII.this.random(SpaceRunIII.this.pfWidth() / 3,
							SpaceRunIII.this.pfWidth()), SpaceRunIII.this
							.random(0.0D, SpaceRunIII.this.pfHeight()), 2,
					"enemy");

			setSpeed(SpaceRunIII.this.random(-0.7D, -0.3D),
					SpaceRunIII.this.random(-0.5D, 0.5D));
		}

		public void move() {
			if (SpaceRunIII.this.random(0.0D, 1.0D) < 0.005D) {
				JGPoint localJGPoint = getCenterTile();
				SpaceRunIII.this.setTile(localJGPoint.x, localJGPoint.y, "#");
			}
		}

	}

	public class Player extends JGObject {
		public Player(double arg2, double arg4, double arg6) {
			super("player", false, arg2, arg4, 1, "ship", 0, 0, arg6, arg6, -1);
		}

		public void move() {
			setDir(0, 0);
			if (SpaceRunIII.this.getKey(SpaceRunIII.this.key_up))
				this.ydir = -1;
			if (SpaceRunIII.this.getKey(SpaceRunIII.this.key_down))
				this.ydir = 1;
			if (SpaceRunIII.this.getKey(SpaceRunIII.this.key_right))
				this.x += SpaceRunIII.this.getGameSpeed() * 3.0D * this.xspeed
						/ 2.0D;
			else
				this.x += SpaceRunIII.this.getGameSpeed() * this.xspeed;
			if (!isOnPF(0, 0))
				SpaceRunIII.this.levelDone();
		}

		public void hit(JGObject paramJGObject) {
			if (and(paramJGObject.colid, 2)) {
				SpaceRunIII.this.lifeLost();
			} else {
				SpaceRunIII.this.score += 5;
				paramJGObject.remove();
				new StdScoring("pts", paramJGObject.x, paramJGObject.y, 0.0D,
						-1.0D, 40, "5 pts", SpaceRunIII.this.scoring_font,
						new JGColor[] { JGColor.red, JGColor.yellow }, 2);
			}
		}

		public void hit_bg(int paramInt) {
			SpaceRunIII.this.lifeLost();
		}

	}

}
